package com.czp.infosharingplatformapp.controller;

import com.czp.infosharingplatformapp.dto.MessageRequest;
import com.czp.infosharingplatformapp.model.Message;
import com.czp.infosharingplatformapp.service.MessageService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import java.util.List;

@RestController
@RequestMapping("/api/messages")
public class MessageController {

    @Autowired
    private MessageService messageService;

    // 发布留言
    @PostMapping
    public Message createMessage(@Valid @RequestBody MessageRequest messageRequest) {
        return messageService.createMessage(
                messageRequest.getContent(),
                messageRequest.getUserId(),
                messageRequest.getParentId());
    }

    // 获取所有留言
    @GetMapping("/all")
    public List<Message> getAllMessages() {
        return messageService.getAllMessages();
    }

    // 根据 userId 获取留言
    @GetMapping("/{userId}")
    public List<Message> getMessagesByUserId(@PathVariable Long userId) {
        return messageService.getMessagesByUserId(userId);
    }

    // 根据 userId 获取未读留言，并将其设置为已读
    @GetMapping("/unread/{userId}")
    public List<Message> getUnreadMessagesByUserId(@PathVariable Long userId) {
        return messageService.getUnreadMessagesByUserId(userId);
    }

    // 删除留言
    @DeleteMapping("/{messageId}")
    public String deleteMessage(@PathVariable Long messageId) {
        messageService.deleteMessage(messageId);
        return "留言已成功删除";
    }
}